package cn.com.fuhuitong.utils

import android.content.Context
import com.contrarywind.interfaces.IPickerViewData
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import java.io.BufferedReader
import java.io.InputStreamReader

object ProvinceListUtils {
    fun getProvinceList(context: Context): List<ProvinceEntity> {
        val builder = StringBuilder()
        val bufferedReader = BufferedReader(InputStreamReader(context.assets.open("region.json")))
        var content: String?
        while (bufferedReader.readLine().also { content = it } != null) {
            builder.append(content)
        }
        bufferedReader.close()
        val provinceArray = Gson().fromJson(builder.toString(), Array<ProvinceEntity>::class.java)
        return provinceArray.asList()
    }

    fun getProvinceUIList(provinceList: List<ProvinceEntity>): ProvinceUIData {
        val cityList = mutableListOf<List<CityEntity>>()
        val areaListList = mutableListOf<List<List<AreaEntity>>>()
        for (province in provinceList) {
            val areaList = mutableListOf<List<AreaEntity>>()
            cityList.add(province.cityList)
            for (city in province.cityList) {
                areaList.add(city.areaList)
            }
            areaListList.add(areaList)
        }
        return ProvinceUIData(provinceList, cityList, areaListList)
    }
}

data class ProvinceEntity(
    val provinceId: Int,
    val provinceName: String = "",
    @SerializedName("citys")
    val cityList: MutableList<CityEntity> = mutableListOf()
) : IPickerViewData {
    override fun getPickerViewText(): String = provinceName
}

data class CityEntity(
    val cityId: Int,
    val cityName: String = "",
    @SerializedName("areas")
    val areaList: MutableList<AreaEntity> = mutableListOf()
) : IPickerViewData {
    override fun getPickerViewText(): String = cityName
}

data class AreaEntity(
    val id: Int,
    val areaName: String = ""
) : IPickerViewData {
    override fun getPickerViewText(): String = areaName
}

data class ProvinceUIData(
    val provinceUIList: List<ProvinceEntity>,
    val cityUIList: List<List<CityEntity>>,
    val areaUIList: List<List<List<AreaEntity>>>
)

class SelectProvinceEntity(
    var provinceId: Int=0,
    var provinceName: String="",
    var cityId: Int=0,
    var cityName: String="",
    var areaId: Int=0,
    var areaName: String=""
)
